package edu.ntu.cloudroid.model.friend;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.jdo.annotations.Embedded;
import javax.jdo.annotations.FetchGroup;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;


import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

import edu.ntu.cloudroid.model.GAEData;
import edu.ntu.cloudroid.model.UserData;
import edu.ntu.cloudroid.model.policy.AbstractPrivacyPreference;
import edu.ntu.cloudroid.model.policy.PolicyInfo;
import edu.ntu.cloudroid.model.profile.ProfileData;
import edu.ntu.cloudroid.server.backendservice.PolicyService;
import edu.ntu.cloudroid.server.backendservice.ProfileService;

@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true")
@FetchGroup(name = GroupData.FETCH_GROUP_NAME, members = {
      @Persistent(name = "memberKeys"), @Persistent(name = "owner") })
public class GroupData implements Serializable, GAEData
{
   private static final long serialVersionUID = 1L;

   public static final String FETCH_GROUP_NAME = "withDetachables";

   @PrimaryKey
   @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
   private Key key;
 
   @Persistent
   private String groupOwnerName;
   
   @Persistent
   private String groupName;

   @Persistent
   private Key policyKey = null;

   @Persistent 
   private int prefId; //privacy ID
   
   public void setPrivacyPrefId(int id){
	   this.prefId = id;
   }
   
   public int getPrivacyPrefId(){
	   return this.prefId;
   }
   
   public Key getGroupPolicyKey() {
	return policyKey;
   }

	public void setGroupPolicy(Key groupPolicyKey) {
		this.policyKey = groupPolicyKey;
	}

   @Persistent
   private Set<String> memberNames = new HashSet<String>();

   public void addMember(String member){
	   this.memberNames.add(member);
   }
   
   public boolean hasAMember(String k){
	   return this.memberNames.contains(k);
   }
   
   public Key getKey()
   {
      return this.key;
   }

   public void setKey(Key key)
   {
      this.key = key;
   }

   public String getGroupOwnerName(){
	   return this.groupOwnerName;
   }
   
   public void setGroupOwnerName(String n){
	   this.groupOwnerName = n;
   }
   
   public String getGroupName()
   {
      return this.groupName;
   }

   public void setGroupName(String name)
   {
      this.groupName = name;
   }  

   public Set<String> getMemberKeys()
   {
      return this.memberNames;
   }

   public void setMemberNames(Set<String> members)
   {
      this.memberNames = members;
   }

   @Override
   public boolean equals(Object o)
   {
      GroupData g = (GroupData) o;
      if (g.groupName.equals(this.groupName)) {
         return true;
      }
      return false;
   }

	@Override
	public UserData getMetaData(Object param) throws Exception {
		GroupInfo gi = new GroupInfo(this.groupOwnerName, this.groupName, this.memberNames, GroupInfo.REPLY);
		AbstractPrivacyPreference app = PolicyService.getGroupPolicy(this);		
		gi.setPolicyInfo((PolicyInfo)app.getMetaData(this.groupName));
		return gi;
	}
}
